// src/router/index.js
import { createRouter, createWebHashHistory } from 'vue-router'

// 初始静态路由（Layout 本身 + 一个默认子路由）
const routes = [
  {
    path: '/',
    name: 'Layout',
    redirect: '/dashboard',
    component: () => import('../layout/AppLayout.vue'),
    children: [
      {
        path: 'dashboard',
        name: '数据统计',
        component: () => import('../views/DataStatistics.vue'),
        meta: { title: '数据统计', icon: 'HomeFilled', show_type: 1 },
      },
      {
        path: 'test-query',
        name: '测试查询参数',
        component: () => import('../views/TestTailwind.vue'),
        meta: {
          title: '测试查询参数',
          icon: 'Document',
          show_type: 1,
          query: { id: 2, type: 'example' },
        },
      },
       {
        path: '/adminEdit',
        name: '管理员编辑',
        meta: {
          title: '管理员编辑',
          icon: 'Document',
          show_type: 2,
        },
        component: () => import('../views/auth/adminEdit.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/auth/Login.vue'),
  },

  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/404.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})
router.beforeEach((to: any, _: any, next: any) => {
  const token = localStorage.getItem('token')

  if (!token && to.path !== '/login') {
    next('/login')
  } else if (token && to.path === '/login') {
    next('/dashboard')
  } else {
    next()
  }
})

export default router
